package com.fenghongzhang.day012;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

/**
 * 1,发送者
 * 2,广播接收者 -> 两种, 1静态 ->清单, 2.动态->Activity的代码.
 *
 *
 */
public class Main2Activity extends AppCompatActivity {

    private MyReceiver6 myReceiver6;
    private LocalBroadcastManager instance;
    private MyReceiver3 myReceiver3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        //本地广播管理者 -> 导包
         instance = LocalBroadcastManager.getInstance(this);
        myReceiver6 = new MyReceiver6();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.ff");
        instance.registerReceiver(myReceiver6,intentFilter);

//        //动态注册一个广播接收者
//         myReceiver3 = new MyReceiver3();
//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction("com.feng");
//        intentFilter.addAction("com.feng1");

        //注册 -> 注销
//        registerReceiver(myReceiver3,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //页面销毁
        unregisterReceiver(myReceiver3);
        instance.unregisterReceiver(myReceiver6);
    }

    public void send(View view) {

        // 1,发送广播
        Intent intent = new Intent();
        intent.setAction("com.feng"); //频道
//        intent.putExtra("")
        Bundle bundle = new Bundle();
        bundle.putString("name","小明");
        intent.putExtras(bundle);
        sendBroadcast(intent);
    }

    public void send2(View view) {
        Intent intent = new Intent();
        intent.setAction("com.feng1");
        sendOrderedBroadcast(intent,null);
    }

    public void send3(View view) {
        Intent intent = new Intent();
        intent.setAction("com.feng3");

        sendBroadcast(intent);
    }

    public void send4(View view) {
        Intent intent = new Intent();
        intent.setAction("com.feng4");

        sendBroadcast(intent);
    }

    public void send5(View view) {

        Intent intent = new Intent();
        intent.setAction("com.ff");
        instance.sendBroadcast(intent);

    }


}
